#coding:utf-8


import sys
from analyze_text_pre import Text
from TextOne import Textone
from TextTwo import Texttwo
from conf import *
from loguru import  logger

class AnalyzeText():
    def __init__(self,**params):
        self.html = params['html']
        self.database = params['database']
        self.config = params['config']
        self.params1 = {'config':self.config,
                        'get_money':params['get_money'],
                        'data_source_id':params['data_source_id'],
                        'clear_solo':params['clear_solo']}

    def get_data_html(self):
        html_replace_one,html_replace_two = Text(**self.params1).read(self.html,self.database)
        return html_replace_one,html_replace_two

    def data_handle(self,html_replace_one,html_replace_two):
        result_one = Textone(**self.params1).part_one_text(html_replace_one)
        result_two = Texttwo(**self.params1).part_two_text(html_replace_two)
        end_dict = {'yeji': result_one, 'zb': result_two}
        yeji_need = ['项目名称', '项目类型', '评标成员', '标段']
        tt_dict = {}
        for yeji_needi in yeji_need:
            tt_dict[yeji_needi] = end_dict['zb'][yeji_needi] if yeji_needi in end_dict['zb'] else ''
        for yeji_needi in yeji_need:
            if yeji_needi not in end_dict['yeji']:
                end_dict['yeji'][yeji_needi] = tt_dict[yeji_needi]
        company_ls = ['第一中标候选人','第二中标候选人','第三中标候选人']
        for yeji_needi in company_ls:
            for _ in end_dict['yeji'][yeji_needi]:
                if '项目名称' not in _:
                    _['项目名称'] =  tt_dict['项目名称']
        del tt_dict
        return end_dict
    def main(self):
        html_replace_one,html_replace_two = self.get_data_html()
        end = self.data_handle(html_replace_one,html_replace_two)
        return end

if __name__ == '__main__':
    conn = pymysql.connect(
        host='192.168.0.160',
        port=3306,
        user='root',
        passwd='lubanjianye',
        charset='utf8',
        database='data_collection_server',
        cursorclass=pymysql.cursors.DictCursor)
    cursor = conn.cursor()
    args = {'conn': conn,
            'cursor': cursor}
    conf = Conf(**args)
    config = conf.main()
    confunc = ConFunc(config)
    get_money = confunc.get_money
    clear_solo = confunc.clear_solo
    params = {'html':'C:/Users/Administrator/Desktop/table_easy/table8.html',
              'database':False,
              'config':config,
              'get_money':get_money,
              'clear_solo':clear_solo,
              'data_source_id':1000075}
    print(AnalyzeText(**params).main())